package Threads.extra;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/*
   new：线程被new出来
   blocked：只有在等待锁的时候，线程会进入blocked状态
   runnable：线程等待调度/正在运行/调用了阻塞方法（io）阻塞，都是runnable状态，因为java的线程是交给os去调度的，所以java中不会区分等待调度/正在运行
   又因为对于java线程来说，等待运行是等待cpu，调用了io方法阻塞是等待io，二者没有什么大区别，所以一律归为runnable状态
   timed_wait:调用了带参数的wait，park方法
   runnable->waiting：wait/join/park
   runnable->timed_wating:带超时参数的sleep,wait,join，park方法
 */
public class state_test {
    //for test1
    public static final Object lock=new Object();
    public static void main(String[] args) throws IOException {
        //test1();
        test2();
    }


    //t1睡眠的时候的状态时timed_waiting,t2得不到锁，blocked
    public static void test1(){
        new Thread(()->{
            synchronized (lock){
                System.out.println("I am waiting---");
                try {
                    Thread.sleep(10000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t1").start();
        new Thread(()->{
            System.out.println("ahahhah");
            synchronized (lock){
            }
        },"t2").start();
    }

    //在调用阻塞函数accept导致阻塞的时候，线程状态仍然是runnable
    public static void test2() throws IOException {
        ServerSocketChannel ssc=ServerSocketChannel.open();
        ssc.bind(new InetSocketAddress("localhost",8888));
        SocketChannel accept = ssc.accept();
    }
}
